Mod Title: XMB Portal Poll Block v1.0

Mod Author: Unknown

Last Updated: 05/12/06

Required: Portal System v2.4   @ http://www.xmbmods.com/

Mod Description:
This mod will provide your portal with a block to display a Poll when a poll fid is entered in to the Portal Settings.

Supported Version: XMB 1.9.5 Nexus Final

Updated for 1.9.5 by: WormHole @ XMB Garage

Installation Note: Before adding this mod to your forum, you should back up all files related to this mod.

License Note: This mod is released under the GPL License.

Author Note:
For security purposes, Please Check: http://www.xmbgarage.com for the latest version of this mod.
Downloading this mod from other sites could cause malicious code to enter into your XMB Forum software.
As such, XMB Garage will not offer support for mods not offered in our Hack Request List.


================
1. Open portal.php:
================

======
Find:
======

'portal_quick_reg',
);

=============
Replace With:
=============

'portal_quick_reg',
'portal_poll'
);

=====
2. Find: 
=====

$sql = $db->query("SELECT p.*, t.* FROM $table_portal_templates p LEFT JOIN $table_templates t ON t.name = p.name WHERE p.status = 'on' ORDER BY p.displayorder ASC  LIMIT 0 , 30");

=================================================
Add Above (if you don't have the Improved Polls hack installed):
=================================================

    // Portal Poll Block Mod Begin
    $queryww = $db->query("SELECT p.*,t.*,f.* FROM ".$table_posts." AS p, ".$table_threads." AS t, ".$table_forums." AS f WHERE p.fid='".$portalpollfid."' AND t.pollopts != '' AND t.tid=p.tid AND f.fid=p.fid GROUP BY (t.tid) ORDER BY p.dateline DESC LIMIT 0, 1");
    $thread = $db->fetch_array($queryww);
    $that = array();
    $poll = '';
    $polltitle = '';
    $subject = stripslashes($thread['subject']);
    $icon = $thread['icon'];
    $tid =  $thread['tid'];
    $fid =  $thread['fid'];
    $icon = "<img src=\"$smdir/$icon\" border=\"0\">";
    $polltitle .= "<font class=\"mediumtxt\"><br /><b>$subject</b><br /><br /></font>";
    $comments = $thread['replies'];
    
     if ($comments == "1"){
        $comment_txt = "comment";
     }else{
        $comment_txt = "Comments";
     }

     if ($comments < 0){
        $comments = 0;
     }
        $comments = "<a href=\"viewthread.php?tid=$tid\">$comments $comment_txt</a> |";
     if($thread['closed'] == "yes") {
        $postcomment = "Closed";
       } else {
        $postcomment = "<a href=\"post.php?action=reply&fid=$portalpollfid&tid=$tid\">Post Comment</a><br />";
     }
    if ($thread['pollopts'] != '' && $thread['pollstatus'] != 'off' && $thread['closed'] != 'yes') {
        $pollbar = '';
        $num = array();
        $pollhtml = '';
        $poll = '';
        $options = explode("#|#", $thread['pollopts']);
        $num_options = count($options);
        if (false !== strpos(' '.$options[$num_options-1].' ', ' '.$xmbuser.' ') || isset($viewresults) == 'yes') {
            //show the 'voted' look
            if (isset($viewresults) == 'yes') {
                $results = "[<a href=\"./portal.php\">$lang[backtovote]</a>]";
            } else {
                $results = '';
            }

            $num_votes = 0;

            for ($i=0; $i < ($num_options-1); $i++) {
                $that = explode('||~|~||', $options[$i]);
                $num_votes += $that[1];
                $poll[$i]['name'] = postify($that[0], 'no', 'no', 'yes', 'no', 'yes', 'yes');
                $poll[$i]['votes'] = $that[1];
            }

            foreach ($poll as $num=>$array) {
                $pollimgnum = 0;
                $pollbar = '';

                if ($array['votes'] > 0) {
                    $orig = round($array['votes']/$num_votes*100, 2);
                    $percentage = round($orig, 2);
                    $poll_length = round($orig/3, 2);
                    $pollbar = str_repeat('<img src="'.$imgdir.'/pollbar.gif" alt="'.$lang['altpollpercentage'].'" />', $poll_length);
                    $percentage .= '%';
                } else {
                    $percentage = '0%';
                }

                $pollhtml .= '
                <tr><td class="tablerow" bgcolor="'.$altbg2.'"">
                <font class="smalltxt">'.$array['name'].'</font>
                <br />
                <img src="'.$imgdir.'/pollbar-s.gif" alt="'.$lang['altpollpercentage'].'" />'.$pollbar.'<img src="'.$imgdir.'/pollbar-e.gif" alt="'.$lang['altpollpercentage'].'" />
                <br />
                <font class="smalltxt">'.$array['votes'].' ('.$percentage.') </font>
                </td>
                </tr>';

            }

            $buttoncode = '';
        } else {
            $results = '[<a href="./portal.php?tid='.$tid.'&viewresults=yes">'.$lang['viewresults'].'</a>]';
            for ($i=0;$i<($num_options-1);$i++) {
                $that = explode('||~|~||', $options[$i]);
                $poll['name'] = postify($that[0], 'no', 'no', 'yes', 'no', 'yes', 'yes');
                $pollhtml .= '<tr><td class="tablerow" bgcolor="'.$altbg2.'" colspan="2"><input type="radio" name="postopnum" value="'.$i.'" /> <font class="smalltxt">'.$poll['name'].'</font></td></tr>';
            }
            
          $buttoncode = '<br /> <div align="center"><input type="submit" class="submit" value="'.$lang['submitvote'].'" /></div>';
        }
        
        $poll = "<form method=\"post\" name=\"input\" action=\"topicadmin.php?action=votepoll&fid=$fid&tid=$tid\">\n";
        $poll .= "$pollhtml<tr><td bgcolor=\"$altbg2\"><center>$buttoncode</center><br />\n";
        $poll .= "<font class=\"smalltxt\"><center>$results<br />$comments $postcomment<br />\n";
        $poll .= "[ <a href=\"forumdisplay.php?fid=$portalpollfid\">Other Polls</a>]</center></font>\n";
        $poll .= "<input type=\"hidden\" name=\"currpoll\" value=\"$thread[pollopts]\"></td></tr></form>\n";

    } elseif ($thread['closed'] == 'yes' && $thread['pollopts'] != '') {
        $pollbar = '';
        $pollhtml = '';

        $options = explode("#|#", $thread['pollopts']);
        $num_options = count($options);
        $num_votes = 0;

        for ($i=0; $i < ($num_options-1); $i++) {
            $that = explode('||~|~||', $options[$i]);
            $num_votes += $that[1];
            $poll[$i]['name'] = postify($that[0], 'no', 'no', 'yes', 'no', 'yes', 'yes');
            $poll[$i]['votes'] = $that[1];
        }
        foreach ($poll as $array) {
            $pollimgnum = 0;
            $pollbar = '';

            if ($array['votes'] > 0) {
                $percentage = round(round(($array['votes'])/$num_votes*100,2)/3, 2);
                for($num = 0; $num < $percentage; $num++) {
                    $pollbar .= '<img src="'.$imgdir.'/pollbar.gif" alt="'.$lang['altpollpercentage'].'" />';
                }
                $percentage .= '%';
            } else {
                $percentage = '0%';
            }
            
            $pollhtml .= '
             <tr><td class="tablerow" bgcolor="'.$altbg2.'" width="30%">
             <font class="smalltxt">'.$array['name'].'</font>
             </td>
             <td class="tablerow" bgcolor="'.$altbg2.'" width="60%">
             <img src="'.$imgdir.'/pollbar-s.gif" alt="'.$lang['altpollpercentage'].'" />'.$pollbar.'<img src="'.$imgdir.'/pollbar-e.gif" alt="'.$lang['altpollpercentage'].'" />
             </td>
             <td class="tablerow" bgcolor="'.$altbg2.'" width="10%">
             <font class="smalltxt">'.$array['votes'].' ('.$percentage.') </font>
             </td>
             </tr>';
        }
        $portalpollfid = '';       
    }
    // Portal Poll Block Mod End

===================================================
Use the code below if you do have the Improved Polls hack installed
===================================================

    // Portal Poll Block Mod Begin
    $queryww = $db->query("SELECT p.*,t.*,f.* FROM ".$table_posts." AS p, ".$table_threads." AS t, ".$table_forums." AS f WHERE p.fid='".$SETTINGS['portalpollfid']."' AND t.pollopts = 'open' AND t.tid=p.tid AND f.fid=p.fid GROUP BY (t.tid) ORDER BY p.dateline DESC LIMIT 0, 1");
    $thread = $db->fetch_array($queryww);

    $poll = '';
    $pollbar = '';
    $pollhtml = '';
    $polltitle = '';
    $subject = stripslashes($thread['subject']);
    $icon = $thread['icon'];
    $tid =  $thread['tid'];
    $fid =  $thread['fid'];
    $icon = "<img src=\"$THEME[smdir]/$icon\" border=\"0\">";
    $polltitle .= "<font class=\"mediumtxt\"><br /><b>$subject</b><br /><br /></font>";
    $comments = $thread['replies'];

    $comment_txt = ($comments == 1 ? "comment" : "Comments");

    if ( $comments < 0 ) {
        $comments = 0;
    }
    $comments = "<a href=\"viewthread.php?tid=$tid\">$comments $comment_txt</a> |";
    if($thread['closed'] == "yes") {
        $postcomment = "Closed";
    } else {
        $postcomment = "<a href=\"post.php?action=reply&fid=$fid&tid=$tid\">Post Comment</a><br />";
    }

    if ( $thread['pollopts'] == 'open' ) { // If polls exist, grab the answers and put them in an array
        $tpolls = array();
        $totalvotes = 0;
        $allvoters = '';
        $querypoll = $db->query("SELECT * FROM ".$tablepre."polls WHERE tid='".$tid."' ORDER BY pod ASC");
        while ($post = $db->fetch_array($querypoll)) {
            if ( !empty($post['answer']) ) {
                $tpolls[] = $post;
            }
            $totalvotes += $post['votes'];
            $allvoters .= $post['voters'].'|#|';
        }
        $db->free_result($querypoll);
    }

    if ($thread['pollopts'] == 'open' && $thread['pollstatus'] != 'off' && $thread['closed'] != 'yes') {

        if ( false !== strpos($allvoters, '|#|'.$self['uid'].'|#|') || $viewresults == 'yes' ) {
            //show the 'voted' look
            if (isset($viewresults) == 'yes') {
                $results = '[<a href="./portal.php">'.$lang['backtovote'].'</a>]';
            } else {
                $results = '';
            }

            foreach ($tpolls as $answer) {
                if ( $answer['votes'] > 0 ) {
                    $orig = round($answer['votes']/$totalvotes*100, 2);
                    $percentage = round($orig, 2);
                    $poll_length = round($orig/3, 2);
                    $pollbar = str_repeat('<img src="'.$THEME['imgdir'].'/pollbar.gif" alt="'.$lang['altpollpercentage'].'" />', $poll_length);
                    $percentage .= '%';
                } else {
                    $pollbar = '';
                    $percentage = '0%';
                }
                $pollhtml .= '
                <tr><td class="tablerow" bgcolor="'.$THEME['altbg2'].'"">
                <font class="smalltxt">'.postify($answer['answer'], 'no', 'no', 'yes', 'no', 'yes', 'yes').'</font>
                <br />
                <img src="'.$THEME['imgdir'].'/pollbar-s.gif" alt="'.$lang['altpollpercentage'].'" />'.$pollbar.'<img src="'.$THEME['imgdir'].'/pollbar-e.gif" alt="'.$lang['altpollpercentage'].'" />
                <br />
                <font class="smalltxt">'.$answer['votes'].' ('.$percentage.') </font>
                </td>
                </tr>';
            }
            $buttoncode = '';
        } else {
            $results = '[<a href="./portal.php?tid='.$tid.'&viewresults=yes">'.$lang['viewresults'].'</a>]';
            foreach ($tpolls as $answer) {;
                $pollhtml .= '<tr><td class="tablerow" bgcolor="'.$THEME['altbg2'].'" colspan="2"><input type="radio" name="postopnum" value="'.$answer['pod'].'" /> <font class="smalltxt">'.postify($answer['answer'], 'no', 'no', 'yes', 'no', 'yes', 'yes').'</font></td></tr>';
            }
            $buttoncode = '<br /> <div align="center"><input type="submit" class="submit" value="'.$lang['submitvote'].'" /></div>';
        }

        $poll = "<form method=\"post\" name=\"input\" action=\"polls.php?action=votepoll&fid=$fid&tid=$tid\">\n";
        $poll .= "$pollhtml<tr><td bgcolor=\"$THEME[altbg2]\" align=\"center\">$buttoncode<br />\n";
        $poll .= "<font class=\"smalltxt\">$results<br />$comments $postcomment<br />\n";
        $poll .= "[ <a href=\"forumdisplay.php?fid=$fid\">Other Polls</a> ]</font>\n";
        $poll .= "<input type=\"hidden\" name=\"currpoll\" value=\"$thread[pollopts]\"></td></tr></form>\n";

    } elseif ( $thread['closed'] == 'yes' && $thread['pollopts'] == 'open' ) {

        foreach ($tpolls as $answer) {
            if ( $answer['votes'] > 0 ) {
                $poll_length = round(round($answer['votes']/$totalvotes*100, 2)/3, 2);
                $pollbar = str_repeat('<img src="'.$THEME['imgdir'].'/pollbar.gif" alt="'.$lang['altpollpercentage'].'" />', $poll_length);
                $percentage .= '%';
            } else {
                $pollbar = '';
                $percentage = '0%';
            }
            $pollhtml .= '
            <tr><td class="tablerow" bgcolor="'.$THEME['altbg2'].'" width="30%">
            <font class="smalltxt">'.postify($answer['answer'], 'no', 'no', 'yes', 'no', 'yes', 'yes').'</font>
            </td>
            <td class="tablerow" bgcolor="'.$THEME['altbg2'].'" width="60%">
            <img src="'.$THEME['imgdir'].'/pollbar-s.gif" alt="'.$lang['altpollpercentage'].'" />'.$pollbar.'<img src="'.$THEME['imgdir'].'/pollbar-e.gif" alt="'.$lang['altpollpercentage'].'" />
            </td>
            <td class="tablerow" bgcolor="'.$THEME['altbg2'].'" width="10%">
            <font class="smalltxt">'.$answer['votes'].' ('.$percentage.') </font>
            </td>
            </tr>';
        }

    }
    // Portal Poll Block Mod End

=======================
3.  Open cp_portal.php:
=======================
=====
Find:
=====

        $SETTINGS['portaltopmembers'] = (int) $SETTINGS['portaltopmembers'];

===========
Add Below:
===========

        $SETTINGS['portalpollfid'] = (int) $SETTINGS['portalpollfid'];

=====
Find:
=====

        $portaltopmembersnew = (int) $portaltopmembersnew;

===========
Add Below:
===========

        $portalpollfidnew = (int) $portalpollfidnew;

=====
Find:
=====

            portalview='$portalviewnew'

============
Replace With:
============

            portalview='$portalviewnew',
            portalpollfid='$portalpollfidnew'


=====================================================
4. create portal block: portal_poll in your portal admin:
=================================================
============================================
insert below info into portal_poll template:
============================================

<table cellspacing="0" cellpadding="0" border="0" width="100%" bgcolor="$bordercolor">
<tr>
<td>
<table border="0" cellspacing="$borderwidth" cellpadding="$tablespace" width="100%">
<tr>
<td class="category"><strong><font color="$cattext">&raquo; <a href="viewthread.php?tid=$tid"> $lang[portalpoll]</b></a></font></strong></td>
</tr>
<td class="ctrtablerow" bgcolor="$altbg2" width="100%"  colspan="2">
$polltitle</td>
</tr>
$poll
</table>
</td>
</tr>
</table>

==============================================================
5.  Go to Admin Panel and open padmin_admin_settings:
=====================================================
=====
Find:
=====

<tr class="tablerow">
<td bgcolor="$THEME[altbg1]" width="50%" valign="top">$lang[portalpostsnull]</td>
<td bgcolor="$THEME[altbg2]"><textarea rows="3" name="portalpostsnullnew" cols="25">$portalpostsnull</textarea></td>
</tr>

==========
Add Above:
==========

<tr class="tablerow">
<td bgcolor="$THEME[altbg1]" width="50%">$lang[portalpollfid]</td>
<td bgcolor="$THEME[altbg2]"><input type="text" name="portalpollfidnew" value="$portalpollfid" size="5" /></td>
</tr>

========================================================================
6. edit file English.portal.lang
==============================================================
============
Find:
============
?>
============
==========
Add Above:
==========
// XMB Portal Poll Block Begin
$lang['portalpoll'] = "Latest Voting Poll";
$lang['portalpollfid'] = "Forum ID (fid) to pull Poll from:";
// XMB Portal Poll Block End
==========
==============================================================
7. Run in Upgrade (Insert Raw Sql) in your admin panel:
==============================================================
==============================================================
ALTER TABLE `$table_settings` ADD `portalpollfid` SMALLINT( 30 ) NOT NULL;
===========================================================================
8.  Now go to Portal Admin Settings and add your fid number of your poll
========================================================================